import Vue from 'vue'
import Vuex from 'vuex'
// cookies组件
import Cookie from 'vue-cookies'
// Vue.prototype.Cookies = Cookie
Vue.use(Vuex)

export default new Vuex.Store({
    //组件中通过 this.$store.state.user_id 调用
    state:{
        user_id:Cookie.get("user_id"),
        token:Cookie.get("token"),
        login:Cookie.get("login")
    },
    mutations: {
      // 组件中通过 this.$store.commit('userLogin', 参数) 调用
      userLogin: function(state, userinfo){
          state.user_id = userinfo.user_id;
          state.token = userinfo.token;
          state.login = true;
          Cookie.set("user_id", userinfo.user_id, "3600min");
          Cookie.set("token", userinfo.token, "3600min");
          Cookie.set("login", false, "3600min");
      },
      userLogOut: function(state){
          state.user_id = null;
          state.token = null;
          state.login = false;
          Cookie.remove('user_id');
          Cookie.remove('token');
          Cookie.remove('login');
      }
    }
})
